<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <!--
                        <el-date-picker v-model="queryForm.timeVal" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="daterange" placement="bottom-end" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" @change="onchangeTime" />
             -->
            <el-form-item>
              <el-select v-model="queryForm.appid" placeholder="请选择app" class="width100" filterable clearable>
                <el-option
                  v-for="item in applist"
                  :key="item.appid"
                  :value="item.appid"
                  :label="item.appname">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item v-if="this.selOptions.JavaInfo.level===0">
              <el-select v-model="queryForm.orgid" placeholder="请选择机构" class="width100" filterable clearable>
                <el-option
                  v-for="item in orglist"
                  :key="item.id"
                  :value="item.id"
                  :label="item.orgname">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>

              <el-select v-model="queryForm.adplatform" placeholder="请选择广告平台" class="width100" filterable clearable>
                <el-option
                  v-for="item in selOptions.adplatform"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>

              <el-select v-model="queryForm.adtype" placeholder="请选择广告类型" class="width100" filterable clearable>
                <el-option
                  v-for="item in selOptions.adType"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>

              <el-select v-model="queryForm.level" placeholder="请选择奖励类型" class="width100" filterable clearable>
                <el-option
                  v-for="item in selOptions.adlevel"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryForm.type" placeholder="请选择收益类型" class="width100" filterable clearable>
                <el-option
                  v-for="item in selOptions.incometype"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>



            <el-form-item>
              <el-input  v-if="this.selOptions.JavaInfo.level===0" v-model="queryForm.ecpm_begin" placeholder="请输入ecpm值" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>

            <el-form-item>
              <el-input v-model="queryForm.adsysid" placeholder="请输入广告id" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.taskid" placeholder="请输入任务ID" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.taskname" placeholder="请输入任务名称" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.userid" placeholder="请输入用户ID" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.username" placeholder="请输入用户名称" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input  v-if="this.selOptions.JavaInfo.level===0" v-model="queryForm.deviceid" placeholder="请输入设备ID" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>

            <el-form-item>
              <el-date-picker v-model="queryForm.timeVal" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small"
                              type="daterange" placement="bottom-end" start-placeholder="开始日期"
                              end-placeholder="结束日期" style="width: 250px;" @change="onchangeTime"
                              :picker-options="pickerOptions" :default-time="['00:00:00', '23:59:59']"
                />
            </el-form-item>

            <el-form-item>
              <el-button @click="search()">查询</el-button>

              <el-button v-if="checkPermi(['deviceadincome:index:delete'])" type="danger" @click="deleteHandle()"
                         :disabled="dataListSelections.length <= 0">删除
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div  style="border-bottom:1px solid #e6ebf5;padding: 0px 10px 20px" v-if="this.selOptions.JavaInfo.level===0&&this.selOptions.JavaInfo.id!==9">
        <div align="middle" >
          <a-row :gutter="this.selOptions.JavaInfo.level===0?24:12" class="ivu-mt">
            <a-col :span="6" v-if="this.selOptions.JavaInfo.level===0">
              <a-statistic title="金币数量"  :value="res.sumcoin"  />
            </a-col>
            <a-col :span="this.selOptions.JavaInfo.level===0?6:12">
              <a-statistic title="收益金币"  :value="res.sumrealcoin" />
            </a-col>
            <a-col :span="6" v-if="this.selOptions.JavaInfo.level===0">
              <a-statistic title="系统抽成" :value="res.sumsyscoin" />
            </a-col>
            <a-col :span="this.selOptions.JavaInfo.level===0?6:8" >
              <a-statistic title="实际收益"  :precision="2" :value="res.sumrealcoin/100000.0+res.sumsyscoin/100000.0" />
            </a-col>
          </a-row>
        </div>
      </div>


      <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                style="width: 100%;" size="mini" :highlight-current-row="true"
                :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
        <!--        <el-table-column prop="id" label="主键" >-->
        <!--        </el-table-column>-->
        <el-table-column prop="deviceid" label="设备ID" width="80px">
        </el-table-column>
        <el-table-column prop="taskname" label="任务信息" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.taskid }} - {{ scope.row.taskname }}
          </template>
        </el-table-column>
        <el-table-column prop="userid" label="用户信息" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.userid }} - {{ scope.row.username }}
          </template>
        </el-table-column>
        <el-table-column prop="adsysid" label="广告信息" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.adplatform }} - {{ scope.row.adsysid }}
            <el-tag type="success" size="mini" v-if="scope.row.adtype==0">开屏广告</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==1">banner广告</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==2">激励视频</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==3">插屏广告</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==4">语音红包</el-tag>
          </template>

        </el-table-column>
        <!--        <el-table-column prop="adtype" label="广告类型" >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column prop="adplatform" label="广告平台" >-->
        <!--        </el-table-column>-->
        <el-table-column prop="ecpm" label="ecpm值" v-if="this.selOptions.JavaInfo.level===0&&this.selOptions.JavaInfo.id!==9">
        </el-table-column>
<!--        <el-table-column prop="ecpmrate" label="ecpm金币比例">-->
<!--        </el-table-column>-->


        <el-table-column prop="coin" label="金币数量" v-if="this.selOptions.JavaInfo.level===0">
        </el-table-column>
        <el-table-column prop="realcoin" label="收益金币">
        </el-table-column>
<!--        <el-table-column prop="sharecoin" label="上级抽成" min-width="100px">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.sharecoin }} <span-->
<!--            style="font-size: 8px;color: red">{{ scope.row.extractionrate }}%</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column prop="ecpmrate" label="系统抽成" min-width="100px" v-if="this.selOptions.JavaInfo.level===0&&this.selOptions.JavaInfo.id!==9">
          <template slot-scope="scope">
            {{ scope.row.syscoin }} <span
            style="font-size: 8px;color: red">{{ scope.row.ecpmrate * 100 }}%</span>
          </template>
        </el-table-column>


        <!--        <el-table-column prop="ruserid" label="设备收益根userid" >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column prop="rid" label="设备收益根id，rid==0，为总计" >-->
        <!--        </el-table-column>-->
        <el-table-column prop="level" label="奖励类型" width="120px">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.level===0">任务完成奖励</el-tag>
            <el-tag size="mini" type="warn" v-if="scope.row.level===1">任务奖励</el-tag>
            <el-tag size="mini" type="success" v-if="scope.row.level===2">下级分润</el-tag>
            <el-tag size="mini" type="success" v-if="scope.row.level===3">下级分润</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="level" label="收益类型" width="120px">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.type===0">用户收益</el-tag>
            <el-tag size="mini" type="warn" v-if="scope.row.type===1">机构收益</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="appname" label="app" width="120" :show-overflow-tooltip="true"/>
        <el-table-column prop="orgname" label="机构" width="120" :show-overflow-tooltip="true"/>
        <el-table-column prop="createtime" label="创建时间" width="150px">
        </el-table-column>
        <!--        <el-table-column prop="updatetime" label="更新时间" >-->
        <!--        </el-table-column>-->

        <!--
<template slot-scope="scope" > <el-tag type="success" v-if="scope.row.type===0">免费</el-tag><el-tag type="warn" v-else>付费</el-tag></template>
<template slot-scope="scope" ><el-image style="width: 80px !important;height: 100% !important"  fit="scaleDown" :src="scope.row.icon" :preview-src-list="[scope.row.icon]"/></template>
-->

        <el-table-column fixed="right" v-if="checkPermi(['deviceadincome:index:update'])&&checkPermi(['deviceadincome:index:delete'])" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" v-if="checkPermi(['deviceadincome:index:update'])" size="small"
                       @click="addOrUpdateHandle(scope.row.id)">修改
            </el-button>
            <el-button type="text" v-if="checkPermi(['deviceadincome:index:delete'])" size="small"
                       @click="deleteHandle(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>


        <div  style="border-bottom:1px solid #e6ebf5;padding: 0px 10px 20px" >
          <div  >
            <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="tableForm.pageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="tableForm.pageSize"
              :total="tableForm.total"
              layout="total,slot, sizes, prev, pager, next, jumper">
              <span class="el-pagination__total">金币收益：{{res.sumrealcoin}}</span>
<!--              <template #default="{ total, from, to }">-->
<!--                &lt;!&ndash; 自定义内容 &ndash;&gt;-->
<!--                <span>{{ `总共 ${total} 条记录, 当前显示 ${from} 到 ${to} 条` }}</span>-->
<!--              </template>-->
            </el-pagination>
          </div>

        </div>
      </div>

    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './deviceadincome-add-and-update'
import api from '@/api/api'
import utils from '@/api/utils'

import { checkPermi } from '@/utils/permission'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      selOptions: {},
      dataList: [],
      applist:[],
      orglist:[],
      res:{},
      pickerOptions: {
        shortcuts: [
          {
            text: '当天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              // end.setTime(start.getTime() + 3600 * 1000 * 24 * 1);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '昨天',
            onClick(picker) {
              const start = new Date();
              const end = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
               end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '前天',
            onClick(picker) {
              const start = new Date();
              const end = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 2);
              end.setTime(end.getTime() - 3600 * 1000 * 24 * 2);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近三天',
            onClick(picker) {
              const start = new Date();
              const end = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
              end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
              picker.$emit('pick', [start, end]);
            }
          },
          {
          text: '最近一周',
          onClick(picker) {
            const start = new Date();
            const end = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
            picker.$emit('pick', [start, end]);
          }
        }, {
            text: '本月',
            onClick(picker) {
              const now = new Date();
              const start = new Date(now.getFullYear(), now.getMonth(), 1);
              const end = new Date();
              // start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
              end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '上月',
            onClick(picker) {
              // const now = new Date();
              const start = new Date(new Date().getFullYear(), new Date().getMonth()-1, 1);
              const end = new Date(new Date().getFullYear(), new Date().getMonth(), 0);
              // start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
              picker.$emit('pick', [start, end]);
            }
          }, {
          text: '最近三个月',
          onClick(picker) {
            const now = new Date();
            const start = new Date(now.getFullYear(), new Date().getMonth()-3, 1);
            const end = new Date(now.getFullYear(), new Date().getMonth(), 0);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      queryForm: {
        id: '',
        adtype: '',
        adplatform: '',
        ecpm_begin: '',
        ecpmrate: '',
        extractionrate: '',
        level: '',
        coin: '',
        adsysid: '',
        taskid: '',
        taskname: '',
        userid: '',
        username: '',
        ruserid: '',
        rid: '',
        deviceid: '',
        orgid: '',
        appid: '',
        type: '',
        createtime: '',
        updatetime: '',
        realcoin: '',
        sharecoin: '',
        createtime_begin: '',
        createtime_end: '',
        timeVal: []
      },
      tableForm: {
        pageIndex: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted() {
    this.selOptions = this.$constants.selOptions
    this.changeh(1)
    this.search()
    utils.getAppList().then(res => {this.applist=res.datalist});
    utils.getOrgList().then(res => {this.orglist=res.datalist});
  },
  methods: {
    checkPermi,
    // 每页数
    sizeChangeHandle(val) {
      this.tableForm.pageSize = val
      this.tableForm.pageIndex = 1
      this.search()
    },
    changeh(timeLable) {
      let start = new Date()
      let end = new Date()
      let type = timeLable
      if (type === 2) {
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
        end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
      } else if (type === 3) {
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 2)
        end.setTime(end.getTime() - 3600 * 1000 * 24 * 2)
      } else if (type === 4) {
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
        end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
      } else if (type === 5) {
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
      } else if (type === 6) {
        start = new Date(start.getFullYear(), start.getMonth(), 1)
        end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
      } else if (type === 7) {
        start = new Date(start.getFullYear(), start.getMonth() - 1, 1)
        end = new Date(new Date().getFullYear(), new Date().getMonth(), 0)
      } else if (type === 8) {
        start = new Date(start.getFullYear(), start.getMonth() - 3, 1)
        end = new Date(new Date().getFullYear(), new Date().getMonth(), 0)
      }

      this.queryForm.timeVal = [start, end]
      // this.queryForm.start = this.formattedTime(start) + ' 00:00:00'
      // this.queryForm.end = this.formattedTime(end) + ' 23:59:59'

      this.queryForm.createtime_begin =this.formattedTime(start)+" 00:00:00"
      this.queryForm.createtime_end =  this.formattedTime(end)+" 23:59:59"

      // this.getStatistics()

    },
    formattedTime(date) {
      const year = date.getFullYear()
      const month = ('0' + (date.getMonth() + 1)).slice(-2)
      const day = ('0' + date.getDate()).slice(-2)
      const hour = ('0' + date.getHours()).slice(-2)
      const minute = ('0' + date.getMinutes()).slice(-2)
      const second = ('0' + date.getSeconds()).slice(-2)
      return `${year}-${month}-${day}`
    },
    // 当前页
    currentChangeHandle(val) {
      this.tableForm.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    change(row) {
      let postData = { id: row.id }
      postData.status = row.status == 1 ? 0 : 1
      api.post('rest/vabc/deviceadincome/updateById', postData).then(res => {
        this.$message.success('操作成功')
        row.status = postData.status
        // this.getDataList()
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let postData = { ids: ids.join(',') }
        api.post('rest/vabc/deviceadincome/delete', postData).then(res => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      })
    },
    search() {
      this.tableForm.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.dataListLoading = true
      let postData = {
        'order': 'desc',
        'column': 'id'
      }
      Object.assign(postData, this.queryForm, this.tableForm)

      postData.taskname = api.getLikeValue(postData.taskname)
      postData.username = api.getLikeValue(postData.username)
      postData.adplatform = api.getLikeValue(postData.adplatform)
      api.post('rest/vabc/deviceadincome/list', postData).then(res => {
        this.res=res
        this.dataList = res.datalist
        this.tableForm.total = res.total
      }).finally(() => {
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    onchangeTime(e) {
      if (e) {
        this.queryForm.createtime_begin = e[0]+" 00:00:00"
        this.queryForm.createtime_end = e[1]+" 23:59:59"
      } else {
        this.queryForm.createtime_begin = ''
        this.queryForm.createtime_end = ''
      }

    }
  }
}
</script>
